package cn.wanho.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.wanho.CmsUserThreadLocal.UserThreadLocal;
import cn.wanho.dao.CmsScoreMapper;
import cn.wanho.dao.CmsUserMapper;
import cn.wanho.dto.CmsUser;
import cn.wanho.rvo.*;
import cn.wanho.service.CmsUserService;
import cn.wanho.util.PassEnDeUtil;
import cn.wanho.util.QiniuUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author OrangeWorld
 */
@Service
public class CmsUserServiceImpl implements CmsUserService {
    @Resource
    private CmsUserMapper cmsUserMapper;

    @Resource
    private CmsScoreMapper cmsScoreMapper;

    private static final String URL = "http://rgb4gi8ky.hn-bkt.clouddn.com/";

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @Override

    public ResultVO modifyPassword(String oldPassword,String newPassword){
        System.out.println(oldPassword);
        System.out.println(newPassword);
        Integer id = UserThreadLocal.get().getId();
        CmsUser user = cmsUserMapper.selectById(id);
        if (PassEnDeUtil.encoder(oldPassword,user.getSalt()).equals(user.getPassword())) {
            cmsUserMapper.updatePassword(id, PassEnDeUtil.encoder(newPassword,user.getSalt()));
            return ResultVO.success(ResultEnum.PASS_UPDATE_SUCCESS.getResCode(), ResultEnum.PASS_UPDATE_SUCCESS.getResMsg());
        }else{
            return ResultVO.failure(ResultEnum.PASS_UPDATE_FAILURE.getResCode(), ResultEnum.PASS_UPDATE_FAILURE.getResMsg());

        }

    }

    /**
     * 创建用户
     * @param userName
     * @param placeId
     */
    @Override
    public ResultVO addUser(String userName, Integer placeId) {
        CmsUser cmsUser1 = cmsUserMapper.selectByUserName(userName);
        System.out.println("==============="+cmsUser1);
        if (cmsUser1==null){
            System.out.println("+++++++++++++++++++++++");
        CmsUser cmsUser = new CmsUser();
        //封装对象
        // 盐值
        String salt = IdUtil.simpleUUID();
        String path = "icon.jpeg";
        // 密码加密
        cmsUser.setPassword(PassEnDeUtil.encoder("123456",salt));
        cmsUser.setSalt(salt);
        cmsUser.setUsername(userName);
        cmsUser.setIcon(path);
        cmsUser.setPlaceId(placeId);
        cmsUser.setParentId(UserThreadLocal.get().getId());
        cmsUser.setIntroduction("none");
        cmsUserMapper.insert(cmsUser);
        return ResultVO.success(ResultEnum.USER_CREATE_SUCCESS.getResCode(),ResultEnum.USER_CREATE_SUCCESS.getResMsg());
        }else{
            return ResultVO.failure(ResultEnum.USER_CREATE_FAILURE.getResCode(),ResultEnum.USER_CREATE_FAILURE.getResMsg());
        }
    }


    /**
     * 查看所有学生成绩（不判断是否授课完结）
     *
     * @param classId
     * @param stageId
     * @param teacherId
     * @param studentName
     */
    @Override
    public ResultVO selectStudentGrade(Integer classId, Integer stageId, Integer teacherId, String studentName, Integer stageNum) throws ParseException {
//        PageHelper.startPage(pageNum,rowNum);
        List<ScoreVO> scoreVOS = cmsScoreMapper.selectStudentGrade(classId, stageId, teacherId, studentName,stageNum);
        if (!scoreVOS.isEmpty()){
//            PageInfo<ScoreVO> pageInfo = new PageInfo<>(scoreVOS);
            return ResultVO.success(ResultEnum.STUDENT_GRADE_SUCCESS.getResCode(),ResultEnum.STUDENT_GRADE_SUCCESS.getResMsg(),scoreVOS);
        }else {
            return ResultVO.failure(ResultEnum.STUDENT_GRADE_FAILURE.getResCode(),ResultEnum.STUDENT_GRADE_FAILURE.getResMsg());
        }
    }

    /**
     * 查询所有老师
     * @param pageNum
     * @param rowsNum
     * @return
     */
    @Override
    public ResultVO selectAllTeacher(Integer pageNum,Integer rowsNum) {
        PageHelper.startPage(pageNum,rowsNum);
        List<TeacherVO> teacherVO = cmsUserMapper.selectAllTeacher();
        PageInfo<TeacherVO> pageInfo = new PageInfo<>(teacherVO);
        return ResultVO.success(ResultEnum.ALL_TEACHER_SELECT_SUCCESS.getResCode(), ResultEnum.ALL_TEACHER_SELECT_SUCCESS.getResMsg(),pageInfo);
    }

    /**
     * 移除老师
     * @param userId
     * @return
     */
    @Override
    public ResultVO removeTeacher(Integer userId){
        int i = cmsUserMapper.removeTeacher(userId);
        if (i>0){
            return ResultVO.success(ResultEnum.UPDATE_TEACHER_INFORMATION_SUCCESS.getResCode(), ResultEnum.UPDATE_TEACHER_INFORMATION_SUCCESS.getResMsg());
        }else {
            return ResultVO.failure(ResultEnum.UPDATE_TEACHER_INFORMATION_FAILURE.getResCode(), ResultEnum.UPDATE_TEACHER_INFORMATION_FAILURE.getResMsg());
        }
    }

    /**
     * 查询没有班级的学生
     * @return
     */
    @Override
    public ResultVO selectAllStudent(Integer pageNum,Integer rowsNum){
        PageHelper.startPage(pageNum,rowsNum);
        List<SimpleStudentVO> studentVOS = cmsUserMapper.selectAllStudent();
        PageInfo<SimpleStudentVO> pageInfo = new PageInfo<>(studentVOS);
        return ResultVO.success(ResultEnum.SELECT_STUDENT_NO_CLASS_SUCCESS.getResCode(),ResultEnum.SELECT_STUDENT_NO_CLASS_SUCCESS.getResMsg(),pageInfo );
    }

    /**
     * 获取老师列表
     * @return
     */
    @Override
    public ResultVO selectAllTeacher2(){
        List<SimpleTeacherVO> simpleTeacherVOS = cmsUserMapper.selectAllTeacher2();
        return ResultVO.success(ResultEnum.GET_TEACHER_SUCCESS.getResCode(), ResultEnum.GET_TEACHER_SUCCESS.getResCode(),simpleTeacherVOS);
    }

}
